package com.rfid.afd.presenter;

import android.os.Handler;

import com.rfid.afd.activity.SetLabelActivity;
import com.rfid.afd.activity.view.ISetLabView;
import com.rfid.util.Convert;
import com.rfid.util.LogUtils;
import com.rfid.util.RfidManager;
import com.rfid.util.RfidUitl;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class SetLabPresenter extends BasePresenter<ISetLabView> implements RfidManager.RfidListener {

    protected Handler mHandler;
    private int mCount;
    private SetLabelActivity.LabBean mBean;
    Executor mExecutor;
    public SetLabPresenter() {
        RfidManager.getInstance().setRfidListener(this);
        mHandler = new Handler();
        mExecutor = Executors.newSingleThreadExecutor();
    }

    public void writeEpc(String barcode) {
        mCount = 0;
        mBean = new SetLabelActivity.LabBean();
        RfidManager.getInstance().getEpc();
        mExecutor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if(mCount == 1) {
                    mBean.barcode = barcode;
                    RfidManager.getInstance().writeLevel(barcode + ",");
                    try {
                        Thread.sleep(400);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    getEpc();
                } else {
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            if(mIview != null) {
                                mIview.onWriteFail();
                            }
                        }
                    });
                }
            }
        });
    }

    public void getEpc() {
        RfidManager.getInstance().getEpc();
    }

    @Override
    public void onRfid(String rfid) {
    }

    @Override
    public void onOperationTag(String epc) {
        mCount ++;
        if(mBean != null && mBean.originEpc == null) {
            mBean.originEpc = epc;
        }
        String level = RfidUitl.decodeLevelEpc(epc);
        if(!RfidUitl.isLevelBarcode(level)) {
            return;
        }
        String finalLevel =  level.replace(",","");
        LogUtils.v("finalLevel = " + finalLevel);
        if(mBean != null && mBean.barcode != null && mBean.barcode.equals(finalLevel)) {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    if(mBean != null && mIview != null) {
                        SetLabelActivity.LabBean bean = mBean;
                        mIview.onWrite(bean);
                        mBean = null;
                    }
                }
            });
        }
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                mIview.onGetEpc(finalLevel);
            }
        });
    }
}
